package kr.co.doriclassic.mypuppystory.kind;

import kr.co.doriclassic.mypuppystory.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

public class KindPuppy extends Activity {

	private KindAdapter mBaseExpandableAdapter = null;
	private ExpandableListView mListView;
	private String[] titles;
	private String[][] kinds;
	private String[][] contents;
	private String[][] imgs;
	private String[][] kindcds;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.network_stringarray);

		setLayout();

		titles = getResources().getStringArray(R.array.titleimg);

		kinds = new String[titles.length][];
		contents = new String[titles.length][];
		imgs = new String[titles.length][];
		kindcds = new String[titles.length][];
		
		for (int i = 0; i < titles.length; i++) {
			int resId = getResources().getIdentifier("kind" + i, "array",
					getPackageName());
			kinds[i] = getResources().getStringArray(resId);
			int resId1 = getResources().getIdentifier("kind_content" + i,
					"array", getPackageName());
			contents[i] = getResources().getStringArray(resId1);
			int resId2 = getResources().getIdentifier("kind_img" + i, "array",
					getPackageName());
			imgs[i] = getResources().getStringArray(resId2);
			int resId3 = getResources().getIdentifier("kind_code" + i, "array",
					getPackageName());
			kindcds[i] = getResources().getStringArray(resId3);
		}

		mBaseExpandableAdapter = new KindAdapter(this, titles, kinds);
		mListView.setAdapter(mBaseExpandableAdapter);

		mListView.setGroupIndicator(null);

		// 차일드 클릭 했을 경우 이벤트
		mListView.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				Intent intent = new Intent(KindPuppy.this, KindIntent.class);
				intent = intent.putExtra("img",
						imgs[groupPosition][childPosition]);
				intent = intent.putExtra("content",
						contents[groupPosition][childPosition]);
				intent = intent.putExtra("kind",
						kinds[groupPosition][childPosition]);
				intent = intent.putExtra("kindcd",
						kindcds[groupPosition][childPosition]);
				startActivity(intent);
				return false;
			}
		});

		// 그룹이 닫힐 경우 이벤트
		mListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
			@Override
			public void onGroupCollapse(int groupPosition) {
				// Toast.makeText(getApplicationContext(),
				// "g Collapse = " + groupPosition, Toast.LENGTH_SHORT)
				// .show();
			}
		});

		// 그룹이 열릴 경우 이벤트
		mListView.setOnGroupExpandListener(new OnGroupExpandListener() {
			@Override
			public void onGroupExpand(int groupPosition) {

				int groupCount = mBaseExpandableAdapter.getGroupCount();

				// 한 그룹을 클릭하면 나머지 그룹들은 닫힌다.
				for (int i = 0; i < groupCount; i++) {
					if (!(i == groupPosition))
						mListView.collapseGroup(i);
				}
			}
		});
	}

	/*
	 * Layout
	 */
	private void setLayout() {
		mListView = (ExpandableListView) findViewById(R.id.listView);
	}

}